/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.Product;
import Util.DBUtil;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author DatPLT
 */
public class ProductDAO {
    public List<Product> GetAllProducts(){
        DBUtil util = new DBUtil();
        Connection conn = null;
        Statement stm = null;
        StringBuilder strBuilder = new StringBuilder();
        List<Product> productList = new ArrayList<Product>();
        ResultSet rs = null;
        Product product = null;

        try {
            Class.forName(util.getDriverName());
            DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
            conn = DriverManager.getConnection(util.getURL(), util.getUsername(), util.getPassword());
            strBuilder.append("SELECT * FROM Products");
//            if (condition != null && !condition.equals("")) {
//                strBuilder.append(condition);
//            }
            stm = conn.createStatement();
            rs = stm.executeQuery(strBuilder.toString());
            productList = new ArrayList<Product>();
            while (rs.next()) {
                product = new Product(BigInteger.valueOf(rs.getInt("ProductId")),rs.getString("Name"),
                        rs.getString("Description"),rs.getBigDecimal("Price"),rs.getString("Image"),BigInteger.valueOf(rs.getInt("CategoryId")));
                productList.add(product);
            }
            return productList;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                stm.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
